//
//  PlayViewController.m
//  CountDown2
//
//  Created by mmac on 11/8/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "PlayViewController.h"
#import "GameData.h"

#import "PlayUIView.h"
#import "CoverView.h"
#import "MainMenuView.h"
#import "AboutView.h"
#import "GameOverView.h"
#import "GameClearView.h"

#import <QuartzCore/QuartzCore.h>

#define FULL_SCREEN_RECT CGRectMake(0, 0, 480, 320);

@implementation PlayViewController

@synthesize playUIView;
@synthesize gameData;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

- (void) removeAllSubviews {
	for (UIView *ssview in [[self view] subviews]) {            
		[ssview removeFromSuperview];
		// [ssview releaes];
	}	
}

- (void) loadGameClear {
	[self removeAllSubviews];
	
	CGRect fullFrame = FULL_SCREEN_RECT;
	GameClearView *aView = [[GameClearView alloc] initWithFrame:fullFrame viewController:self];
	
	NSLog(@"add new gameclear view");
	[self.view addSubview:aView];
}

- (void) loadGameOver{

	
	[self removeAllSubviews];
	
	CGRect fullFrame = FULL_SCREEN_RECT;
	GameOverView *aView = [[GameOverView alloc] initWithFrame:fullFrame viewController:self];
	
	NSLog(@"add new gameover view");
	[self.view addSubview:aView];
}

- (void) loadMainMenu{
	[self removeAllSubviews];
	
	CGRect fullFrame = FULL_SCREEN_RECT;
	MainMenuView *mainMenuView = [[MainMenuView alloc] initWithFrame:fullFrame viewController:self];
	
	NSLog(@"add new mainmenu view");
	[self.view addSubview:mainMenuView];
}

- (void) loadCover{
	[self removeAllSubviews];
	
	CGRect fullFrame = FULL_SCREEN_RECT;
	CoverView *coverView = [[CoverView alloc] initWithFrame:fullFrame viewController:self];
	
	NSLog(@"add new cover view");
	[self.view addSubview:coverView];
}

- (void) loadAbout{
	
	[self removeAllSubviews];
	
	CGRect fullFrame = FULL_SCREEN_RECT;
	AboutView *aboutView = [[AboutView alloc] initWithFrame:fullFrame viewController:self];
	
	NSLog(@"add new about view");
	[self.view addSubview:aboutView];
}

- (void)loadLevel {
	
	NSLog(@"load level with data %@", gameData.data);
	
	NSDictionary *level;
	level = [gameData getLevel];

	NSLog(@"level %@", level);
	
	// draw play view
	
	
	CATransition *transition = [CATransition animation];
    // Animate over 3/4 of a second
    transition.duration = 0.75;
    // using the ease in/out timing function
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    // Now to set the type of transition. Since we need to choose at random, we'll setup a couple of arrays to help us.
    // {kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};
    // {kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};
    
	transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
 
    
    // Finally, to avoid overlapping transitions we assign ourselves as the delegate for the animation and wait for the
    // -animationDidStop:finished: message. When it comes in, we will flag that we are no longer transitioning.
    transition.delegate = self;
    
    // Next add it to the containerView's layer. This will perform the transition based on how we change its contents.
    [[[self view] layer] addAnimation:transition forKey:nil];
	
	/*
	CGContextRef context = UIGraphicsGetCurrentContext();
	[UIView beginAnimations:nil context:context];
	[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
						   forView:self.view cache:YES];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDuration:2.0];
	*/
	
	for (UIView *ssview in [[self view] subviews]) {            
		[ssview removeFromSuperview];
		// [ssview releaes];
	}
	
	// if(playUIView){
	//	NSLog(@"remove old view");
	//	[playUIView removeFromSuperview];
	//	[playUIView release];
	// }
	
	CGRect fullFrame = FULL_SCREEN_RECT;
	PlayUIView *playUIView2 = [[PlayUIView alloc] initWithFrame:fullFrame viewController:self levelData:level];

	NSLog(@"add new playuiview view");
	[self.view addSubview:playUIView2];
	

	// [UIView commitAnimations];

	 
	// playUIView = playUIView2;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSLog(@"view did load");
	
	gameData = [[GameData alloc] init];
	
	
	
	[self loadCover];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[gameData release];
	// [playUIView release];
    [super dealloc];
}


@end
